]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
screenshot: Add include_cursor parameter
authorAdel Gadllah <adel.gadllah@gmail.com>
Tue, 14 Feb 2012 18:24:41 +0000 (19:24 +0100)
committerAdel Gadllah <adel.gadllah@gmail.com>
Tue, 14 Feb 2012 20:10:08 +0000 (21:10 +0100)
Add a boolean parameter to Screenshot and ScreenshotWindow which draws the cursor on the screenshot when set
to true.

https://bugzilla.gnome.org/show_bug.cgi?id=670086

js/ui/shellDBus.js
src/shell-global-private.h
src/shell-global.c
src/shell-global.h

index f74e8220b3f58fa47c2d60008ffc27f953653da6..96c75f1d43b41fa3b6b43c566bff20ce6b218d97 100644 (file)
@@ -39,11 +39,13 @@ const GnomeShellIface = <interface name="org.gnome.Shell">
 </method>
 <method name="ScreenshotWindow">
     <arg type="b" direction="in" name="include_frame"/>
+    <arg type="b" direction="in" name="include_cursor"/>
     <arg type="b" direction="in" name="flash"/>
     <arg type="s" direction="in" name="filename"/>
     <arg type="b" direction="out" name="success"/>
 </method>
 <method name="Screenshot">
+    <arg type="b" direction="in" name="include_cursor"/>
     <arg type="b" direction="in" name="flash"/>
     <arg type="s" direction="in" name="filename"/>
     <arg type="b" direction="out" name="success"/>
@@ -134,6 +136,7 @@ const GnomeShell = new Lang.Class({
      * @y: The Y coordinate of the area
      * @width: The width of the area
      * @height: The height of the area
+     * @flash: Whether to flash the area or not
      * @filename: The filename for the screenshot
      *
      * Takes a screenshot of the passed in area and saves it
@@ -151,6 +154,8 @@ const GnomeShell = new Lang.Class({
     /**
      * ScreenshotWindow:
      * @include_frame: Whether to include the frame or not
+     * @include_cursor: Whether to include the cursor image or not
+     * @flash: Whether to flash the window area or not
      * @filename: The filename for the screenshot
      *
      * Takes a screenshot of the focused window (optionally omitting the frame)
@@ -159,8 +164,8 @@ const GnomeShell = new Lang.Class({
      *
      */
     ScreenshotWindowAsync : function (params, invocation) {
-        let [include_frame, flash, filename] = params;
-        global.screenshot_window (include_frame, filename, 
+        let [include_frame, include_cursor, flash, filename] = params;
+        global.screenshot_window (include_frame, include_cursor, filename, 
                                   Lang.bind(this, this._onScreenshotComplete, 
                                             flash, invocation));
     },
@@ -168,6 +173,8 @@ const GnomeShell = new Lang.Class({
     /**
      * Screenshot:
      * @filename: The filename for the screenshot
+     * @include_cursor: Whether to include the cursor image or not
+     * @flash: Whether to flash the screen or not
      *
      * Takes a screenshot of the whole screen and saves it
      * in @filename as png image, it returns a boolean
@@ -175,8 +182,8 @@ const GnomeShell = new Lang.Class({
      *
      */
     ScreenshotAsync : function (params, invocation) {
-        let [flash, filename] = params;
-        global.screenshot(filename, 
+        let [include_cursor, flash, filename] = params;
+        global.screenshot(include_cursor, filename, 
                           Lang.bind(this, this._onScreenshotComplete, 
                                     flash, invocation));
     },
index 5f2faf845b96cef4bc0635f8e5dab4ff0f49158b..27e782921caa08843520749cc2ff4d61752dfd7e 100644 (file)
@@ -28,6 +28,8 @@ typedef struct _screenshot_data {
   cairo_surface_t *image;
   cairo_rectangle_int_t screenshot_area;
 
+  gboolean include_cursor;
+
   ShellGlobalScreenshotCallback callback;
 } _screenshot_data;
 
index 2e7e20075e01cc0d41ebe3388eff485277352a30..8588f3726fddb9f7529f3b37e4e5a4d051060ed9 100644 (file)
@@ -1948,6 +1948,61 @@ do_grab_screenshot (_screenshot_data *screenshot_data,
                                data, (cairo_destroy_func_t)g_free);
 }
 
+static void
+_draw_cursor_image (cairo_surface_t *surface,
+                    cairo_rectangle_int_t area)
+{
+  XFixesCursorImage *cursor_image;
+
+  cairo_surface_t *cursor_surface;
+  cairo_region_t *screenshot_region;
+  cairo_t *cr;
+
+  guchar *data;
+  int stride;
+  int i, j;
+
+  cursor_image = XFixesGetCursorImage (clutter_x11_get_default_display ());
+
+  if (!cursor_image)
+    return;
+
+  screenshot_region = cairo_region_create_rectangle (&area);
+
+  if (!cairo_region_contains_point (screenshot_region, cursor_image->x, cursor_image->y))
+    {
+       XFree (cursor_image);
+       cairo_region_destroy (screenshot_region);
+       return;
+    }
+
+  cursor_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, cursor_image->width, cursor_image->height);
+
+  /* The pixel data (in typical Xlib breakage) is longs even on
+   * 64-bit platforms, so we have to data-convert there. For simplicity,
+   * just do it always
+   */
+  data = cairo_image_surface_get_data (cursor_surface);
+  stride = cairo_image_surface_get_stride (cursor_surface);
+  for (i = 0; i < cursor_image->height; i++)
+    for (j = 0; j < cursor_image->width; j++)
+      *(guint32 *)(data + i * stride + 4 * j) = cursor_image->pixels[i * cursor_image->width + j];
+
+  cairo_surface_mark_dirty (cursor_surface);
+
+  cr = cairo_create (surface);
+  cairo_set_source_surface (cr,
+                            cursor_surface,
+                            cursor_image->x - cursor_image->xhot - area.x,
+                            cursor_image->y - cursor_image->yhot - area.y);
+  cairo_paint (cr);
+
+  cairo_destroy (cr);
+  cairo_surface_destroy (cursor_surface);
+  cairo_region_destroy (screenshot_region);
+  XFree (cursor_image);
+}
+
 static void
 grab_screenshot (ClutterActor *stage,
                  _screenshot_data *screenshot_data)
@@ -2003,6 +2058,9 @@ grab_screenshot (ClutterActor *stage,
   screenshot_data->screenshot_area.width = width;
   screenshot_data->screenshot_area.height = height;
 
+  if (screenshot_data->include_cursor)
+    _draw_cursor_image (screenshot_data->image, screenshot_data->screenshot_area);
+
   g_signal_handlers_disconnect_by_func (stage, (void *)grab_screenshot, (gpointer)screenshot_data);
 
   result = g_simple_async_result_new (NULL, on_screenshot_written, (gpointer)screenshot_data, grab_screenshot);
@@ -2031,6 +2089,7 @@ grab_area_screenshot (ClutterActor *stage,
 /**
  * shell_global_screenshot:
  * @global: the #ShellGlobal
+ * @include_cursor: Whether to include the cursor or not
  * @filename: The filename for the screenshot
  * @callback: (scope async): function to call returning success or failure
  * of the async grabbing
@@ -2041,6 +2100,7 @@ grab_area_screenshot (ClutterActor *stage,
  */
 void
 shell_global_screenshot (ShellGlobal  *global,
+                         gboolean include_cursor,
                          const char *filename,
                          ShellGlobalScreenshotCallback callback)
 {
@@ -2050,6 +2110,7 @@ shell_global_screenshot (ShellGlobal  *global,
   data->global = global;
   data->filename = g_strdup (filename);
   data->callback = callback;
+  data->include_cursor = include_cursor;
 
   stage = CLUTTER_ACTOR (meta_plugin_get_stage (global->plugin));
 
@@ -2104,6 +2165,7 @@ shell_global_screenshot_area (ShellGlobal  *global,
  * shell_global_screenshot_window:
  * @global: the #ShellGlobal
  * @include_frame: Whether to include the frame or not
+ * @include_cursor: Whether to include the cursor or not
  *
  * @filename: The filename for the screenshot
  * @callback: (scope async): function to call returning success or failure
@@ -2116,6 +2178,7 @@ shell_global_screenshot_area (ShellGlobal  *global,
 void
 shell_global_screenshot_window (ShellGlobal  *global,
                                 gboolean include_frame,
+                                gboolean include_cursor,
                                 const char *filename,
                                 ShellGlobalScreenshotCallback callback)
 {
@@ -2166,6 +2229,9 @@ shell_global_screenshot_window (ShellGlobal  *global,
   stex = META_SHAPED_TEXTURE (meta_window_actor_get_texture (META_WINDOW_ACTOR (window_actor)));
   screenshot_data->image = meta_shaped_texture_get_image (stex, &clip);
 
+  if (include_cursor)
+    _draw_cursor_image (screenshot_data->image, screenshot_data->screenshot_area);
+
   result = g_simple_async_result_new (NULL, on_screenshot_written, (gpointer)screenshot_data, shell_global_screenshot_window);
   g_simple_async_result_run_in_thread (result, write_screenshot_thread, G_PRIORITY_DEFAULT, NULL);
   g_object_unref (result);
index 6456c9e743fcd92122c42fc43e3e2e8009735e06..7e8345129d8946047544ac0a9279c2d4efde66d2 100644 (file)
@@ -154,10 +154,12 @@ void    shell_global_screenshot_area           (ShellGlobal  *global,
 
 void    shell_global_screenshot_window         (ShellGlobal  *global,
                                                 gboolean include_frame,
+                                                gboolean include_cursor,
                                                 const char *filename,
                                                 ShellGlobalScreenshotCallback callback);
 
 void    shell_global_screenshot                (ShellGlobal  *global,
+                                                gboolean include_cursor,
                                                 const char *filename,
                                                 ShellGlobalScreenshotCallback callback);
 typedef enum {